#import "DDChooseViewController.h"
#import "DDChooseBtn.h"
#import "DDChooseCell.h"
#import "DDChooseTips.h"
@interface DDChooseViewController ()<JTWChooseBtnDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) DDChooseBtn *changeBtn;
@property (nonatomic, strong) DDChooseBtn *insertBtn;
@property (nonatomic, strong) DDChooseTips *tips;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UIView *line;
@end
@implementation DDChooseViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (DDChooseBtn *)changeBtn
{
    if (!_changeBtn)
    {
        _changeBtn = [[DDChooseBtn  alloc] initWithTitle:@"欢迎登录:" btnTitle:@"切换"];
        _changeBtn.delegate = self;
        _changeBtn.tag = 200;
        [self.ddview addSubview:_changeBtn];
        _changeBtn.c_top.equal(self.ddview.bar.c_bottom).with.coffset(GAP);
        _changeBtn.c_left.equal(self.ddview).with.coffset(GAP);
        _changeBtn.c_right.equal(self.ddview).with.coffset(-GAP);
        _changeBtn.c_height.equal(60);
    }
    return _changeBtn;
}
- (DDChooseBtn *)insertBtn
{
    if (!_insertBtn)
    {
        _insertBtn = [[DDChooseBtn  alloc] initWithTitle:@"选择小号进入游戏" btnTitle:@"增加"];
        _insertBtn.delegate = self;
        _insertBtn.tag = 201;
        [self.ddview addSubview:_insertBtn];
        _insertBtn.c_top.equal(self.line);
        _insertBtn.c_left.equal(self.ddview).with.coffset(GAP);
        _insertBtn.c_right.equal(self.ddview).with.coffset(-GAP);
        _insertBtn.c_height.equal(60);
    }
    return _insertBtn;
}
- (DDChooseTips *)tips
{
    if (!_tips)
    {
        _tips = [[DDChooseTips alloc] init];
        [self.ddview addSubview:_tips];
    }
    return _tips;
}
- (UIView *)line
{
    if (!_line)
    {
        _line = [[UIView alloc] init];
        [self.ddview addSubview:_line];
        _line.c_left.equal(self.changeBtn).with.coffset(GAP);
        _line.c_right.equal(self.changeBtn).with.coffset(-GAP);
        _line.c_top.equal(self.changeBtn.c_bottom);
        _line.c_height.equal(1);
    }
    return _line;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        [_collectionView registerClass:[DDChooseCell class] forCellWithReuseIdentifier:@"DDChooseCell"];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.ddview addSubview:_collectionView];
        _collectionView.c_top.equal(self.insertBtn.c_bottom).with.coffset(GAP);
        _collectionView.c_left.equal(self.insertBtn);
        _collectionView.c_right.equal(self.insertBtn);
        _collectionView.c_bottom.equal(self.ddview).with.coffset(-GAP);
    }
    return _collectionView;
}
- (UICollectionViewFlowLayout *)layout
{
    if (!_layout)
    {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = GAP;
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return _layout;
}
- (CGSize)yjd_cellItemSize:(UIInterfaceOrientation)orientation
{
    CGFloat width = 0;
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
        width = (SCREEN_WIDTH - GAP * 3) / 2.0;
    else
        width = SCREEN_WIDTH - GAP * 2;
    return CGSizeMake(width , JTWCHOOSECELLHEIGHT);
}
- (void)yjd_screenRotate:(UIInterfaceOrientation)orientation
{
    self.layout.itemSize = [self yjd_cellItemSize:orientation];
    if (self.tips.hidden == NO)
    {
        self.tips.c_edges.equal(UIEdgeInsetsMake(SCREEN_HEIGHT, 0, -SCREEN_HEIGHT, 0));
        self.tips.transform = CGAffineTransformMakeTranslation(0, -SCREEN_HEIGHT);
    }
}
#pragma mark - JTWChooseBtnDelegate method
- (void)JTWChooseBtnWithEvent:(JTWChooseBtnEvent)event tag:(NSInteger)tag
{
    if (event == JTWChooseBtnEventBtn)
    {
        if (tag == 200)
        {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else
        {
            [self presentViewController:[NSClassFromString(@"DDXiaoHaoViewController") new] animated:YES completion:nil];
        }
    }
    else
    {
        self.tips.hidden = NO;
        self.tips.content = [NSString stringWithFormat:@"1.您可以在一个游戏里创建最多10个小号。\n2.各小号相互独立，等同于一个%@账号在每个游戏里可以创建多个子账号。\n3.在天际App交易中心，可以将小号出售，同时不影响其他小号正常游戏。",helpermanger().getPara().name];
        self.tips.c_edges.equal(UIEdgeInsetsMake(SCREEN_HEIGHT, 0, -SCREEN_HEIGHT, 0));
        [UIView animateWithDuration:0.3 animations:^{
            self.tips.transform = CGAffineTransformMakeTranslation(0, -SCREEN_HEIGHT);
        }];
    }
}
#pragma mark CollectionViewDelegate&DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellId = @"DDChooseCell";
    DDChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.title = [NSString stringWithFormat:@"第%ld个账号",indexPath.row+1];
    cell.backgroundColor = helpermanger().getPara().color;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DeLog(@"点击第%ld个",(long)indexPath.row+1);
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.changeBtn.content = @"skl10032";
    self.insertBtn.imageName = @"help";
    self.line.backgroundColor = CELL_LINE_COLOR;
    self.collectionView.hidden = NO;
    self.layout.itemSize = [self yjd_cellItemSize:[[UIApplication sharedApplication] statusBarOrientation]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)sp_checkInfo {
    NSLog(@"Continue");
}
@end
